<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Request;

use App\Constants\ErrorCode;
use App\Exception\BussinessException;
use Hyperf\Contract\ValidatorInterface;
use Hyperf\Validation\Request\FormRequest;

class UploadRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return [
            'file' => 'required|file|size:20480|mimes:jpeg,jpg,png',
        ];
    }

    public function messages(): array
    {
        return [
            'file.required' => '请选择上传文件',
            'file.file' => '文件类型错误',
            'file.size' => '文件过大，上传失败',
            'file.mimes' => '只支持jpeg,jpg,png文件类型',
        ];
    }

    protected function failedValidation(ValidatorInterface $validator)
    {
        $msg = $validator->errors()->first();
        throw new BussinessException(ErrorCode::UPLOAD_ERROE, $msg);
    }
}
